﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Tebbee.Business.Server
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CategorySer" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CategorySer.svc or CategorySer.svc.cs at the Solution Explorer and start debugging.
    public class CategorySer : ICategorySer
    {
        private readonly TebbeeContext context;

        public CategorySer()
        {
            context = new TebbeeContext();
        }

        public Category_Get_Result Category_Get(int catId)
        {
            return context.Category_Get(catId).Single();
        }

        public ICollection<Category_Search_Result> Category_Search(string searchString, int currentPage, out int totalPage)
        {
            ObjectParameter ttPage = new ObjectParameter("TotalPage", typeof(Int32));
            var tmp = context.Category_Search(currentPage, searchString, ttPage).ToList();
            totalPage = (Int32)ttPage.Value;
            return tmp;
        }

        public int AddNewCategory(Category category)
        {
            ObjectParameter ReturnId = new ObjectParameter("ReturnId", typeof(Int32));
            context.Category_Add(category.Name,category.Description,category.IconPath, ReturnId);
            return (Int32)ReturnId.Value;
        }


        public ICollection<Category> ListCategories(int startRow, string searchString)
        {
            
            var returnCat = new List<Category>();
            context.Category_List(startRow, searchString).ToList().ForEach(e => returnCat.Add(new Category()
            {
                CatId = e.CatId,
                Description = e.Description,
                IconPath = e.IconPath,
                Name = e.Name
            }));

            return returnCat;
        }


        public int EditCategory(Category category)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.Category_Edit(category.CatId,category.Name,category.Description,category.IconPath,rowEffect);
            return (Int32)rowEffect.Value;
        }


        public int DeleteCategory(int id)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.Category_Delete(id,rowEffect);
            return (Int32)rowEffect.Value;
        }


        public ICollection<Category> AllCategories()
        {
            var returnCat = new List<Category>();
            context.Category_All().ToList().ForEach(e => returnCat.Add(new Category()
            {
                CatId = e.CatId,
                Description = e.Description,
                IconPath = e.IconPath,
                Name = e.Name
            }));

            return returnCat;
        }
    }
}
